
package Interface;

import Utilitarios.conexao;
import javax.swing.JOptionPane;

/**
 *
 * @author Ramon
 */
public class Cadastro_usuarios extends javax.swing.JFrame {

    conexao banco=new conexao();
    /** Creates new form Cadastro_usuarios */
    public Cadastro_usuarios() {
        initComponents();

        banco.conecta();

        try
        {
            //Select*from usuario order by codigo
            banco.executeSQL("Select * from \"User_1\" order by \"Mat_user\" ");

        }catch(Exception error){JOptionPane.showMessageDialog(null, error);}
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        JL_nome_usuario = new javax.swing.JLabel();
        JL_matricula = new javax.swing.JLabel();
        JL_tipo = new javax.swing.JLabel();
        JT_nome_usuario = new javax.swing.JTextField();
        JT_matricula_usuario = new javax.swing.JTextField();
        JC_tipo = new javax.swing.JComboBox();
        JB_limpar = new javax.swing.JButton();
        JT_senha = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        JB_Salvar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Login");
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        setMaximizedBounds(new java.awt.Rectangle(400, 400, 400, 400));
        setName("Login"); // NOI18N
        setResizable(false);

        JL_nome_usuario.setText("Nome.:");

        JL_matricula.setText("Matricula.:");

        JL_tipo.setText("Tipo.:");

        JT_nome_usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JT_nome_usuarioActionPerformed(evt);
            }
        });

        JT_matricula_usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JT_matricula_usuarioActionPerformed(evt);
            }
        });

        JC_tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Professor", "Administrador" }));

        JB_limpar.setText("Limpar campos");
        JB_limpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_limparActionPerformed(evt);
            }
        });

        JT_senha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JT_senhaActionPerformed(evt);
            }
        });

        jLabel1.setText("Senha.:");

        jLabel2.setText("Falha na conexão");

        JB_Salvar.setText("Salvar");
        JB_Salvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_SalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(JL_matricula)
                            .addComponent(JL_nome_usuario)
                            .addComponent(jLabel1)
                            .addComponent(JL_tipo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(JC_tipo, 0, 298, Short.MAX_VALUE))
                            .addComponent(JT_nome_usuario, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
                            .addComponent(JT_matricula_usuario, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
                            .addComponent(JT_senha, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                        .addComponent(JB_limpar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JB_Salvar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JL_nome_usuario)
                    .addComponent(JT_nome_usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JL_matricula)
                    .addComponent(JT_matricula_usuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JT_senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JC_tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(JL_tipo))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(JB_limpar)
                            .addComponent(JB_Salvar)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)))
                .addContainerGap())
        );

        setBounds(500, 200, 406, 241);
    }// </editor-fold>//GEN-END:initComponents

    private void JT_nome_usuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JT_nome_usuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JT_nome_usuarioActionPerformed

    private void JT_matricula_usuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JT_matricula_usuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JT_matricula_usuarioActionPerformed

    private void JT_senhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JT_senhaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JT_senhaActionPerformed

    private void JB_limparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_limparActionPerformed

      JT_matricula_usuario.setText("");
      JT_nome_usuario.setText("");
      JT_senha.setText("");


    }//GEN-LAST:event_JB_limparActionPerformed

    private void JB_SalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_SalvarActionPerformed

        

        try
        {

            String SQL;
            int tipo=0;
            if(JC_tipo.getSelectedItem().equals("Professor")) 
            {
                tipo=2;
            }else tipo=1;


            SQL="INSERT INTO \"User_1\"(\"Mat_user\", \"Name_user\", \"Level_access\", senha)VALUES ('"+JT_matricula_usuario.getText()+
                    "', '"+JT_nome_usuario.getText()+"',"+tipo+", '"+JT_senha.getText()+"')";
            if(banco.conecta())
            {
                
                
                    banco.statement.executeUpdate(SQL);
                    JOptionPane.showMessageDialog(null,"Cadastro Realizado com sucesso!","Aviso",JOptionPane.INFORMATION_MESSAGE);
            }
        }catch(Exception error)
        {
            JOptionPane.showMessageDialog(null, error);
        }

    }//GEN-LAST:event_JB_SalvarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Cadastro_usuarios().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_Salvar;
    private javax.swing.JButton JB_limpar;
    private javax.swing.JComboBox JC_tipo;
    private javax.swing.JLabel JL_matricula;
    private javax.swing.JLabel JL_nome_usuario;
    private javax.swing.JLabel JL_tipo;
    private javax.swing.JTextField JT_matricula_usuario;
    private javax.swing.JTextField JT_nome_usuario;
    private javax.swing.JTextField JT_senha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

}
